<template>
  <div class="login_container">
    <div class="login_box">
      <!--头像区域 -->
      <div class="avator">
        <img src="../assets/logo.png" alt />
      </div>
      <!--登录表单区域 -->
      <el-form
        :model="LoginForm"
        :rules="LoginFormRules"
        class="loginform"
        ref="LoginFormRef"
      >
        <!--ref引用 -->
        <el-form-item prop="username">
          <!--用户名区域 -->
          <el-input
            prefix-icon="iconfont icon-yonghudenglu"
            v-model="LoginForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <!--密码区域 -->
          <el-input
            prefix-icon="iconfont icon-mima"
            v-model="LoginForm.password"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <!--登录按钮区域 -->
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="restform">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  // 数据区域
  data () {
    return {
      // 登录表单的数据对象
      LoginForm: {
        username: '',
        password: ''
      },
      // 这是表单的验证规则对象
      LoginFormRules: {
        // 验证用户名 属性
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ],
        // 验证密码 属性
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  // 行为区
  methods: {
    // 点击重置表单
    restform () {
      console.log(this)
      this.$refs.LoginFormRef.resetFields()
    },
    // 登录
    login () {
      // 表单预校验
      this.$refs.LoginFormRef.validate(async valid => {
        console.log(valid)
        if (!valid) return
        let { data: result } = await this.$http.post('login', this.LoginForm)
        console.log(result)
        if (result.meta.status !== 200) {
          console.log('登录失败')
          this.$message.error('登录失败！')
          return false
        }
        console.log('登录成功！')
        this.$message.success('登录成功！')
        // 1.将登录成功后的token ，保存到客户端的sessionstorage中
        window.sessionStorage.setItem('token', result.data.token)
        // 2.通过编程式导航，跳转到后台主页
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avator {
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}

.btns {
  display: flex;
  justify-content: flex-end;
}

.loginform {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
}</style
>>
